package org.byd.dmsja.controller.app;

import org.byd.dmsja.entity.DmsParaDb;
import org.byd.dmsja.service.DmsParaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class AppController {
    @Autowired
    private DmsParaService dmsParaService;

    public static class CInitPIn {
        public Timestamp clientTime = null;
    }

    public static class CInitPOut {
        public List<DmsParaDb> paraList = new ArrayList<>();
        public Timestamp clientTime = null;
        public Timestamp serverTime = null;
        public String errMsg = "";
    }

    @PostMapping("/app/init")
    public CInitPOut init(@RequestBody CInitPIn in) {
        CInitPOut out = new CInitPOut();

        // 获取指定时区的当前时间
        ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime nowInShanghai = ZonedDateTime.now(shanghaiZoneId);
        Timestamp timestampShanghaiZone = Timestamp.from(nowInShanghai.toInstant());
        try {
            out.paraList = dmsParaService.findAllPara();
            out.clientTime = in.clientTime;
            out.serverTime = timestampShanghaiZone;
        } catch (Exception e) {
            out.errMsg = e.getMessage();
        }
        return out;
    }

}

